﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Grinko.Lists
{
    public class MyDimList : IList
    {
        #region Constructors

        /// <summary>
        /// Empty list constructor
        /// </summary>
        public MyDimList() : this(new int[0]) { }

        /// <summary>
        /// Parametrized list constructor
        /// </summary>
        /// <param name="x">Array for list inisialisation</param>
        public MyDimList(int[] x)
        {
            m_size = x.Count() + 1;
            l = new int[m_size];
            for (int i = 0; i < m_size - 1; i++)
            {
                l[i] = x[i];
            }
            p = m_size - 1;
        }

        #endregion

        #region IList implementation

        /// <summary>
        /// Clear list
        /// </summary>
        public void Clear()
        {
            m_size = 0;
            l = null;
        }

        /// <summary>
        /// Add element to the end of list
        /// </summary>
        /// <param name="value">Element value</param>
        public void Add(int value)
        {
            CheckCapacity();
            l[p + 1] = value;
            p++;
        }

        /// <summary>
        /// Get list item by index
        /// </summary>
        /// <param name="index">Index value</param>
        /// <returns>Item value</returns>
        public int GetItem(int index)
        {
            return l[index];
        }

        /// <summary>
        /// Amount of elements in array
        /// </summary>
        public int Count
        {
            get
            {
                return p;
            }
        }

        /// <summary>
        /// Arrays compare
        /// </summary>
        /// <param name="comp">array to compare</param>
        /// <returns>result of compare</returns>
        public bool Compare(int[] comp)
        {
            if (comp == null)
                throw new ArgumentNullException();

            if (comp.Length != this.Count)
                return false;

            for (int i = 0; i < this.Count; i++)
            {
                if (comp[i] != GetItem(i))
                    return false;
            }

            return true;
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Resize array
        /// </summary>
        private void Resize()
        {
            m_size = m_size * 2;
            int[] newArray = new int[m_size];
            System.Array.Copy(l, 0, newArray, 0, p);
            l = newArray;
        }

        /// <summary>
        /// Check capacity of list
        /// </summary>
        private void CheckCapacity()
        {
            if (p >= m_size - 1) { Resize(); };
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Size of array
        /// </summary>
        private int m_size;

        /// <summary>
        /// Array
        /// </summary>
        private int[] l;

        /// <summary>
        /// Pointer to the last position
        /// </summary>
        private int p;

        #endregion
    }
}
